package com.example.controller;

import com.common.result.Result;
import com.example.service.DishService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.pojo.dto.DishDTO;
import com.pojo.dto.QueryPageDTO;
import com.pojo.entity.Dish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * **/
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
            dishService.save(dishDTO);
        return Result.success("新增菜品成功");
    }

    /**
     * 菜品分页查询
     * **/
    @GetMapping("/page")
    public Result page(QueryPageDTO queryPageDTO){
        Page<Dish> page = dishService.page(queryPageDTO);
        PageInfo<Dish> dishPageInfo = page.toPageInfo();
        return Result.success(dishPageInfo);
    }

    /**
     * 批量删除
     * **/
    @DeleteMapping
    public Result deleteDishList(Long[] ids){
        System.out.println(ids);
        dishService.deleteArr(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品和对应的口味
     * **/
    @GetMapping("/{id}")
    public Result getDishAndFlavorById(@PathVariable("id") Long id){
        DishDTO dishDTO = dishService.getDishAndFlavorById(id);
        System.out.println(dishDTO.toString());
        return Result.success(dishDTO);
    }

    /**
     * 修改菜品和口味
     * **/
    @PutMapping
    public Result update(@RequestBody DishDTO dish){
        dishService.update(dish);
        return Result.success();
    }

}
